package com.atguigu.study.MyJunit;


//我们自定义了@AtguiguTest注解，
//将注解加入到需要测试的方法上，
//这个测试类通过反射检查，哪些方法加了@Atguigutest将会自动进行单元测试

import cn.hutool.core.date.DateUtil;
import com.atguigu.study.annotations.AtguiguTest;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

public class AutoTestClient {
    public static void main(String[] args) {
        CalcHelpDemo calcHelpDemo = new CalcHelpDemo();
        int para1 =10;
        int para2 = 2;

        AtomicInteger bugCount = new AtomicInteger();

        String filePath = "BugReport"+ (DateUtil.format(new Date(), "yyyyMMddHHmmss"))+".txt";

        Method[] methods = calcHelpDemo.getClass().getMethods();

        for (int i = 0; i < methods.length; i++) {
            if (methods[i].isAnnotationPresent(AtguiguTest.class)){//判断方法上是否有@AtguiguTest注解
                try {
                    methods[i].invoke(calcHelpDemo,para1,para2);
                }catch (Exception e){
                    bugCount.getAndIncrement();
                    log.info("异常名称:{},异常原因:{}",e.getCause().getClass().getSimpleName(),e.getCause().getMessage());

                    FileUtil.writeString(methods[i].getName()+"\t"+"出现了异常"+"\n", filePath, "UTF-8");
                    FileUtil.appendString("异常名称:"+e.getCause().getClass().getSimpleName()+"\n", filePath, "UTF-8");
                    FileUtil.appendString("异常原因:"+e.getCause().getMessage()+"\n", filePath, "UTF-8");
                }finally {
                    FileUtil.appendString("异常数:"+bugCount.get()+"\n", filePath, "UTF-8");
                    System.out.println("bug报告路径: "+"target/classes/XXXXXXX.txt");
                }
            }
        }
    }
}
